<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>Flutter--Image组件 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">Flutter--Image组件</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">February 7, 2020&nbsp;&nbsp;20:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Flutter/">Flutter</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h2 id="一、Flutter-图片组件"><a href="#一、Flutter-图片组件" class="headerlink" title="一、Flutter 图片组件"></a>一、Flutter 图片组件</h2><p>图片组件是显示图像的组件，Image 组件有很多构造函数，记录两个常用的：</p>
<ul>
<li>Image.asset 本地图片 </li>
<li>Image.network 远程图片</li>
</ul>
<h3 id="Image-组件的常用属性"><a href="#Image-组件的常用属性" class="headerlink" title="Image 组件的常用属性:"></a>Image 组件的常用属性:</h3><table>
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>alignment</td>
<td>Alignment</td>
<td>图片的对齐方式</td>
</tr>
<tr>
<td>color  <br> 和  <br> colorBlendMode</td>
<td></td>
<td>设置图片的背景颜色，通常和 colorBlendMode 配合一起 使用，这样可以是图片颜色和背景色混合。上面的图片就 是进行了颜色的混合，绿色背景和图片红色的混合</td>
</tr>
<tr>
<td>fit</td>
<td>BoxFit</td>
<td>fit属性用来控制图片的拉伸和挤压，这都是根据父容器来的。 <br> BoxFit.fill:全图显示，图片会被拉伸，并充满父容器。 <br> BoxFit.contain:全图显示，显示原比例，可能会有空隙。 <br> BoxFit.cover：显示可能拉伸，可能裁切，充满（图片要 充满整个容器，还不变形）。 <br> BoxFit.fitWidth：宽度充满（横向充满），显示可能拉伸， 可能裁切。 <br> BoxFit.fitHeight ：高度充满（竖向充满）,显示可能拉 伸，可能裁切。 <br> BoxFit.scaleDown：效果和 contain 差不多，但是此属 性不允许显示超过源图片大小，可小不可大。</td>
</tr>
<tr>
<td>repeat</td>
<td>平铺</td>
<td>ImageRepeat.repeat: 横向和纵向都进行重复，直到铺满整 个画布。  <br>  ImageRepeat.repeatX: 横向重复，纵向不重复。 <br> ImageRepeat.repeatY：纵向重复，横向不重复。</td>
</tr>
<tr>
<td>width</td>
<td>宽度</td>
<td>一般结合 ClipOval 才能看到效果</td>
</tr>
<tr>
<td>height</td>
<td>高度</td>
<td>一般结合 ClipOval 才能看到效果</td>
</tr>
</tbody></table>
<p>更多属性参考：<a href="https://api.flutter.dev/flutter/widgets/Image-class.html" target="_blank" rel="noopener">https://api.flutter.dev/flutter/widgets/Image-class.html</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">returnCenter( </span><br><span class="line">	child:Container( </span><br><span class="line">		child:Image.network( </span><br><span class="line">			<span class="string">"http://pic.baike.soso.com/p/20130828/20130828161137-1346445960.jpg"</span>, </span><br><span class="line">			alignment:Alignment.topLeft, </span><br><span class="line">			color:Colors.red, </span><br><span class="line">			colorBlendMode:BlendMode.colorDodge, </span><br><span class="line">			<span class="comment">//repeat:ImageRepeat.repeatX, </span></span><br><span class="line">			fit:BoxFit.cover, </span><br><span class="line">			),</span><br><span class="line">		width:<span class="number">300.0</span>, </span><br><span class="line">		height:<span class="number">400.0</span>, </span><br><span class="line">		decoration:BoxDecoration( color:Colors.yellow ),</span><br><span class="line">	),</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h2 id="二、Flutter-引入本地图片"><a href="#二、Flutter-引入本地图片" class="headerlink" title="二、Flutter 引入本地图片"></a>二、Flutter 引入本地图片</h2><p><img src="https://raw.githubusercontent.com/zhangpingV5/BlogPic/master/Flutter--Image%E7%BB%84%E4%BB%B6/1.png" alt=""></p>
<p>然后，打开 pubspec.yaml 声明一下添加的图片文件，注意要配置对</p>
<p><img src="https://raw.githubusercontent.com/zhangpingV5/BlogPic/master/Flutter--Image%E7%BB%84%E4%BB%B6/2.png" alt=""></p>
<p>最后，在代码中就可以用了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">child:Container( </span><br><span class="line">	child:Image.asset(</span><br><span class="line">		<span class="string">"images/a.jpeg"</span>, </span><br><span class="line">		fit:BoxFit.cover </span><br><span class="line">	),</span><br><span class="line">	width:<span class="number">300.0</span>, </span><br><span class="line">	height:<span class="number">300.0</span>, </span><br><span class="line">	decoration:BoxDecoration( </span><br><span class="line">		color:Colors.yellow ),</span><br><span class="line">),</span><br></pre></td></tr></table></figure>

<h2 id="三、Flutter-实现圆角以及实现圆形图片"><a href="#三、Flutter-实现圆角以及实现圆形图片" class="headerlink" title="三、Flutter 实现圆角以及实现圆形图片"></a>三、Flutter 实现圆角以及实现圆形图片</h2><p>实现圆角图片</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> Center(</span><br><span class="line">	child: Container(</span><br><span class="line">		width: <span class="number">300.0</span>,</span><br><span class="line">		height: <span class="number">300.0</span>,</span><br><span class="line">		decoration: BoxDecoration(</span><br><span class="line">			color: Colors.yellow,</span><br><span class="line">			borderRadius: BorderRadius.circular(<span class="number">150</span>),</span><br><span class="line">			image: DecorationImage(</span><br><span class="line">			image: <span class="keyword">new</span> NetworkImage(<span class="string">'https://raw.githubusercontent.com/zhangpingV5/BlogPic/master/Flutter--Image%E7%BB%84%E4%BB%B6/2.png'</span>),</span><br><span class="line">			fit: BoxFit.cover</span><br><span class="line">			)</span><br><span class="line">		),</span><br><span class="line">	),</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>实现圆形图片</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">returnCenter( </span><br><span class="line">	child:Container( </span><br><span class="line">		child:ClipOval( </span><br><span class="line">			child:Image.network(<span class="string">"https://raw.githubusercontent.com/zhangpingV5/BlogPic/master/Flutter--Image%E7%BB%84%E4%BB%B6/2.png"</span>, </span><br><span class="line">				width:<span class="number">150.0</span>, </span><br><span class="line">				height:<span class="number">150.0</span>, </span><br><span class="line">				),</span><br><span class="line">			)</span><br><span class="line">	),</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2020/02/07/Flutter--Image%E7%BB%84%E4%BB%B6/">https://zhangpingv5.gitee.io/2020/02/07/Flutter--Image%E7%BB%84%E4%BB%B6/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/Flutter/"># Flutter</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2020/02/08/Flutter--ListView%E7%BB%84%E4%BB%B6/">Flutter--ListView组件</a>
            
            
            <a class="next" rel="next" href="/2020/02/06/Flutter--Container%E7%BB%84%E4%BB%B6%E3%80%81Text%E7%BB%84%E4%BB%B6/">Flutter--Container组件、Text组件</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
